package com.ruoyi.web.controller.api.user;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.PublishConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.system.domain.PUserBrokerageRecord;
import com.ruoyi.system.domain.PUserExtract;
import com.ruoyi.system.service.IPUserBrokerageRecordService;
import com.ruoyi.system.service.IPUserExtractService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 推广人
 * @Author: wk
 * @Date: 2022/7/26 10:25
 * @Version: 1.0
 */

@Api(value = "推广-api", tags = {"前端-推广分销-api"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/promoter")
public class PromoterApiController {
    private final ISysUserService userService;
    private final IPUserBrokerageRecordService brokerageRecordService;
    private final IPUserExtractService ipUserExtractService;

    @GetMapping("/get-down-user")
    @ApiOperation(value = "我的团队")
    public TableDataInfo<SysUser> getDownUser(SysUser user, PageQuery pageQuery) {
        user.setUserType(PublishConstants.USER_TYPE);
        user.setSpreadId(LoginHelper.getUserId());
        return userService.selectPublicList(user, pageQuery);
    }

    @GetMapping("/get-info")
    @ApiOperation(value = "获取用户信息")
    public R<SysUser> getInfo() {
        return R.ok(userService.selectUserFontById(LoginHelper.getUserId()));
    }

    @GetMapping("/get-fen")
    @ApiOperation(value = "分销订单记录")
    public TableDataInfo<PUserBrokerageRecord> getFen(PUserBrokerageRecord bo, PageQuery pageQuery) {
        bo.setUid(LoginHelper.getUserId());
        return brokerageRecordService.queryMyLogList(bo, pageQuery);
    }

    @PostMapping("/binding-user")
    @ApiOperation(value = "绑定推广人")
    public R<String> bindingPromoter(@RequestBody SysUser sysUser) {
        if (ObjectUtil.isEmpty(sysUser) || ObjectUtil.isEmpty(sysUser.getSpreadId())) {
            return R.fail("必要参数不能为空！");
        }

        userService.bindingPromoter(sysUser.getSpreadId());

        return R.ok("绑定成功！");
    }

    @GetMapping("/getMyTeam")
    @ApiOperation(value = "分销推广-我的团队")
    public R<List<SysUser>> getMyTeam() {
        return R.ok(this.userService.getMyTeam());
    }

    @GetMapping("/getDistributionCenter")
    @ApiOperation(value = "分销中心")
    public R<SysUser> getDistributionCenter() {
        return R.ok(this.userService.getDistributionCenter());
    }

    @GetMapping("/getWithdrawalDetail")
    @ApiOperation(value = "提现明细")
    public TableDataInfo<PUserExtract> getWithdrawalDetail(PageQuery pageQuery) {
        return this.ipUserExtractService.getWithdrawalDetail(pageQuery);
    }
}
